package com.wutianyi.servlet.text;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.wltea.analyzer.help.CharacterHelper;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.wutianyi.text.TextUtils;
import com.wutianyi.utils.HtmlUtils;
import com.wutianyi.utils.NetUtils;

/**
 * @author wutianyi
 * 
 *         用户拉取url内容，并且进行词语分析
 */
public class ParseTextServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4188504357136016861L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String urlStr = NetUtils.getParameterString(req, "urls", "");
		System.out.println(urlStr);
		PrintWriter pw = resp.getWriter();
		if (StringUtils.isBlank(urlStr)) {
			pw.print(0);
		}

		String[] urls = urlStr.split("\n");
		for (String url : urls) {

			String content = HtmlUtils.getBodyString(url, "UTF-8");
			int len = content.length();
			StringBuilder builder = new StringBuilder(len);
			for (int i = 0; i < len; i++) {
				char c = content.charAt(i);
				if (CharacterHelper.isCJKCharacter(c) || c == '\r') {
					builder.append(c);
				}
			}
			try {
				System.out.println(builder.toString());
				int count = TextUtils.updateRelations(builder.toString());
				pw.println(url + " --- word: " + count);
			} catch (EntityNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
